<!-- eslint-disable vue/multi-word-component-names -->
<template>
  <div class="login-container">
    <div class="login-form">
      <el-card class="login-card">
        <el-row class="logo-row">
          <img width="120" height="80" src="/src/assets/logo/loginLogo.png" alt="logo"/>
        </el-row>

        <div class="login-divider"></div>

        <el-form
          label-position="top"
          :model="loginForm"
          class="login-form-content"
        >
          <el-form-item label="UserName:" prop="userName">
            <el-input
              v-model="loginForm.userName"
              class="login-input"
              placeholder="Please enter your username"
              :prefix-icon="UserIcon"
              @keyup.enter="login"
            />
          </el-form-item>

          <el-form-item label="Password:" prop="pwd">
            <el-input
              v-model="loginForm.pwd"
              type="password"
              class="login-input"
              placeholder="Please enter your password"
              :prefix-icon="LockIcon"
              show-password
              @keyup.enter="login"
            />
          </el-form-item>
        </el-form>

        <div class="forgot-password">
          <el-link type="primary">Forgot password?</el-link>
        </div>

        <div v-if="loginErrorMessage" class="login-error">
          <span>{{ loginErrorMessage }}</span>
        </div>

        <el-button
          type="primary"
          :loading="loadLogin"
          class="login-button"
          @click="login"
        >
          Login
        </el-button>

        <div class="create-account">
          <el-link type="primary">Create an account here!</el-link>
        </div>
      </el-card>
    </div>
  </div>
</template>


<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { useRouter } from 'vue-router';
import type { LoginFormVo } from './LoginForm';
import { userAuthProcessStore } from '@/stores/userAuthProcess';
// import { ElMessage} from 'element-plus'
import { authStore } from '@/stores/authStore';
import { User as UserIcon, Lock as LockIcon } from '@element-plus/icons-vue'
const router = useRouter();
const store = authStore();
const loginStore = userAuthProcessStore();
const loginForm = ref<LoginFormVo>({
  userName : '',
  pwd : ''
});
const loginErrorMessage = ref<string>('');
const loadLogin = ref<boolean>(false);
const login = async ()=>{
    loginErrorMessage.value = '';
    if(loginFormValidate()){
      loadLogin.value = true;
      loginForm.value.pwd = loginForm.value.pwd.trim();
      loginForm.value.userName = loginForm.value.userName.trim();
      await loginStore.Login_Account(loginForm.value).catch(err=>{
        loginErrorMessage.value = err;
        loadLogin.value = false;
        return Promise.reject();
      })
      await loginStore.Get_UserInfo().catch(err=>{
        loginErrorMessage.value = err;
        loadLogin.value = false;
        return Promise.reject();
      })
      await loginStore.GET_CODE().catch(err=>{
        loginErrorMessage.value = err;
        loadLogin.value = false;
        return Promise.reject();
      })
      // ElMessage.success("登录成功")
      setTimeout(()=>{
        router.push({ name: 'mainInterface' });
      },1500)
    }
  };
  const loginFormValidate = ()=>{
    if(!loginForm.value.userName.trim()){
      loginErrorMessage.value = "请您输入用户名"
      return false;
    }
    if(!loginForm.value.pwd.trim()){
      loginErrorMessage.value = "请您输入密码"
      return false;
    }
    return true;
  }
  //初始化界面 清空用户信息
  onMounted(async () => {
    //存在token 则清空。
    if(store.getToken){
      await loginStore.Logout();
    }
  })
</script>


<style scoped>
/* 首先修改根容器样式 */
.login-container {
  width: 100%;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(rgba(231, 233, 219, 0.9), rgba(231, 233, 219, 0.9)),
              url("/src/assets/background/login-background.jpg") no-repeat center center;
  background-size: cover;
  padding: 0;
  margin: 0;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.login-form {
  width: 100%;
  max-width: 450px;
  padding: 0 20px;
  box-sizing: border-box;
}

.login-card {
  backdrop-filter: blur(10px);
  background-color: rgba(255, 255, 255, 0.95);
  border-radius: 15px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
  max-height: 90vh;
  box-sizing: border-box;
}

.logo-row {
  text-align: center;
  padding: 15px 0;
  box-sizing: border-box;
}

.login-divider {
  margin: 10px 0;
  border-top: 1px solid rgba(0, 0, 0, 0.1);
}

.login-form-content {
  padding: 0 20px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  box-sizing: border-box;
}

.login-input {
  height: 50px;
  transition: all 0.3s ease;
  width: 100%;
}

.login-input :deep(.el-input__wrapper) {
  background-color: #f8f9fa;
  border-radius: 8px;
  box-shadow: none;
  border: 1px solid #e9ecef;
  padding: 0 15px;
}

.login-input :deep(.el-input__inner) {
  height: 100%;
  color: #606266;
  font-size: 15px;
  box-shadow: 0 0 0 1000px #f8f9fa inset !important;
  -webkit-box-shadow: 0 0 0 1000px #f8f9fa inset !important;
  -webkit-text-fill-color: #606266 !important;
  transition: background-color 5000s ease-in-out 0s;
}

.login-input :deep(.el-input__inner::placeholder) {
  color: #909399;
  font-size: 14px;
}

.login-input :deep(.el-input__prefix-inner) {
  font-size: 18px;
  color: #909399;
  display: flex;
  align-items: center;
}

.login-input :deep(.el-input__prefix-inner svg) {
  width: 18px;
  height: 18px;
}

.login-input :deep(.el-input__wrapper:hover) {
  border-color: var(--el-color-primary);
  box-shadow: 0 0 0 1px var(--el-color-primary-light-8);
}

.login-input :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 2px rgba(var(--el-color-primary-rgb), 0.2);
  border-color: var(--el-color-primary);
}

.login-input :deep(.el-input__suffix-inner) {
  font-size: 16px;
  color: #909399;
}

.login-input :deep(.el-form-item__label) {
  padding-bottom: 8px;
  color: #606266;
  font-size: 15px;
  font-weight: 500;
}

.forgot-password {
  text-align: right;
  padding: 5px 20px;
}

.login-button {
  width: calc(100% - 40px);
  height: 50px;
  margin: 15px 20px;
  font-size: 16px;
  font-weight: 600;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(var(--el-color-primary-rgb), 0.3);
}

.create-account {
  text-align: center;
  padding: 10px 20px 15px;
}

.login-error {
  margin: 10px 20px;
  padding: 12px;
  background-color: #fef0f0;
  color: #f56c6c;
  border-radius: 8px;
  font-size: 14px;
  text-align: center;
  animation: shake 0.5s ease-in-out;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}

@media screen and (max-width: 768px) {
  .login-form {
    width: 90%;
    height: auto;
  }

  .login-card {
    margin: 0;
    height: auto;
    min-height: auto;
  }

  .login-form-content {
    padding: 0 15px;
  }

  .login-input {
    height: 45px;
  }

  .login-button {
    margin: 15px;
    height: 45px;
  }
}

/* 防止内容溢出 */
:deep(.el-form-item) {
  margin-bottom: 12px;
}

/* 添加全局样式 */
:deep(*) {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

/* 修改body样式 */
:global(body) {
  margin: 0;
  padding: 0;
  overflow: hidden;
}

/* 处理自动填充时的背景色 */
.login-input :deep(.el-input__inner:-webkit-autofill) {
  -webkit-box-shadow: 0 0 0 1000px #f8f9fa inset !important;
  -webkit-text-fill-color: #606266 !important;
}

/* 处理自动填充激活时的背景色 */
.login-input :deep(.el-input__inner:-webkit-autofill:focus) {
  -webkit-box-shadow: 0 0 0 1000px #f8f9fa inset !important;
  -webkit-text-fill-color: #606266 !important;
}
</style>
